package com.example.fw;

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;

public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public void addNewContact() {
	    click(By.linkText("add new"));
	}

	public void fillContactForm(ContactData contacts) {
	type(By.name("firstname"), contacts.fname);	
	type(By.name("lastname"), contacts.lname);	
	type(By.name("address"), contacts.address1);
	type(By.name("home"), contacts.homeNumber);
	type(By.name("mobile"), contacts.mobileNumber);
	type(By.name("work"), contacts.workNumber);
	type(By.name("email"), contacts.mail1);
	type(By.name("email2"), contacts.mail2);
	selectByText(By.name("bday"), contacts.dayOfBirth);
	selectByText(By.name("bmonth"), contacts.monthOfBirth); 
	//selectByText(By.name("new group", "group 1"); 
	type(By.name("byear"), contacts.yearOfBirth);
	type(By.name("address2"), contacts.address2);
	type(By.name("phone2"), contacts.phone2);

	}


	public void submitContactCreation() {
		click(By.name("submit"));
	}
	
	public void gotoMainPage() {
		click(By.linkText("home page"));
	}

	
	public void initEditContact(int index) {
		click(By.xpath("(//img[@alt='Edit'])["+(index+1)+"]"));	
	}
	
	public void submitContactRemoval() {
		click(By.xpath("(//input[@name='update'])[2]"));
		
	}

	public void submitContactUpdate() {
		click(By.name("update"));
		
	}

	public List<ContactData> getContacts() {
		List<ContactData> contacts = new ArrayList<ContactData>();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			ContactData contact = new ContactData();
			String title = checkbox.getAttribute("title");
			contact.fname = title.substring("Select (".length (), title.length() - ")".length());
			contact.lname = title.substring("Select (".length (), title.length() - ")".length());
			contacts.add(contact);
		} 
		
		return contacts;
	}
	
}
